package
{
	import flash.desktop.Clipboard;
	import flash.desktop.ClipboardFormats;
	import flash.desktop.NativeDragManager;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NativeDragEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	/**
	 * ...
	 * @author 杨伟华
	 */
	public class Main extends Sprite
	{
		private var pathSting:String = "";
		private var offsetM:uint = 0;
		private var offsetP:Number = 0;
		private var offsetMT:TextField;
		private var offsetPT:TextField;
		private var pCondition:int;
		private var pConditionT:TextField;
		private var pathT:TextField;
		private var acceptBtn:SimpleButton;
		
		private var urlLoader:URLLoader;
		
		public function Main():void
		{
			if (stage)
				init();
			else
				addEventListener(Event.ADDED_TO_STAGE, init);
		
		}
		
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			initUI();
			//urlLoader.load(new URLRequest(""));
			initDrag();
		
		}
		
		private function initDrag():void
		{
			//NativeDragManager.doDrag;
			this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragIn);
			this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDrop);
		}
		
		private function onDrop(event:NativeDragEvent):void
		{
			var dropfiles:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
			var currentFile:File = dropfiles[0];
			pathSting = currentFile.url;
			pathT.text = decodeURI(pathSting);
		}
		
		private function onDragIn(event:NativeDragEvent):void
		{
			var transferable:Clipboard = event.clipboard;
			if (transferable.hasFormat(ClipboardFormats.FILE_LIST_FORMAT) && event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT)[0].nativePath.split(".").pop() == "xml")
			{
				NativeDragManager.acceptDragDrop(this);
			}
			else
			{
				trace("不接受的格式");
			}
		}
		
		private function initUI():void
		{
			var name1:TextField = new TextField();
			name1.text = "m偏移量(整数)";
			name1.x = 20;
			name1.y = 10;
			offsetMT = new TextField();
			offsetMT.type = TextFieldType.INPUT;
			offsetMT.border = true;
			offsetMT.y = name1.y;
			offsetMT.x = 100;
			offsetMT.height = 20;
			addChild(name1)
			addChild(offsetMT);
			
			var name3:TextField = new TextField();
			name3.text = "当m等于：";
			name3.x = 250;
			name3.y = 10;
			pConditionT = new TextField();
			pConditionT.type = TextFieldType.INPUT;
			pConditionT.border = true;
			pConditionT.y = name3.y;
			pConditionT.x = 300;
			pConditionT.height = 20;
			addChild(name3)
			addChild(pConditionT);
			
			var name2:TextField = new TextField();
			name2.text = "时，t偏移量(浮点)";
			name2.x = 400;
			name2.y = 10;
			offsetPT = new TextField();
			offsetPT.type = TextFieldType.INPUT;
			offsetPT.border = true;
			offsetPT.y = name2.y;
			offsetPT.x = 500;
			offsetPT.height = 20;
			addChild(name2)
			addChild(offsetPT);
			
			pathT = new TextField();
			pathT.border = true;
			pathT.y = 50;
			pathT.x = 20;
			pathT.width = 600;
			pathT.height = 40;
			addChild(pathT);
			
			var acceptT:TextField = new TextField();
			acceptT.border = true;
			acceptT.text = "更改吗";
			acceptT.selectable = false;
			acceptT.width = 40;
			acceptT.height = 40;
			var acceptT2:TextField = new TextField();
			acceptT2.border = true;
			acceptT2.text = "更改吧";
			acceptT2.selectable = false;
			acceptT2.width = 40;
			acceptT2.height = 40;
			acceptBtn = new SimpleButton(acceptT, acceptT2, acceptT2, acceptT2);
			acceptBtn.y = pathT.y;
			acceptBtn.x = 650;
			addChild(acceptBtn);
			acceptBtn.addEventListener(MouseEvent.MOUSE_DOWN, acceptBtn_mouseDown);
			urlLoader = new URLLoader();
			urlLoader.addEventListener(Event.COMPLETE, fun);
		}
		
		private function acceptBtn_mouseDown(e:MouseEvent):void
		{
			offsetM = parseInt(offsetMT.text);
			offsetP = parseFloat(offsetPT.text);
			offsetP = isNaN(offsetP) ? 0 : offsetP;
			pCondition = pConditionT.text ? parseInt(pConditionT.text) : -1;
			pathSting = pathT.text;
			if (pathSting)
			{
				fun();
			}
		
		}
		
		private function fun():void
		{
			var file:File = new File(pathSting);
			var fs:FileStream = new FileStream();
			fs.open(file, FileMode.READ);
			var str:String = fs.readMultiByte(file.size, "utf-8");
			fs.close();
			file.moveTo(new File(pathSting.replace(/.xml/g, "_" + new Date().time + ".xml")));
			var data:XML;
			try
			{
				data = new XML(str);
			}
			catch (e:Error)
			{
				data = new XML(str.substr(1));
			}
			finally
			{
				var xmlList:XMLList = data..*;
				var length:uint = xmlList.length();
				var cid:uint = 0;
				var xml:XML;
				for (var i:uint = 0; i < length; i++)
				{
					xml = xmlList[i];
					var result:Boolean = false;
					var mainId:Number = XMLUtils.getPropNumber(xml, '@m');
					var mainPos:Number = TimeFormat.hhmmss2ss(XMLUtils.getPropString(xml, "@t"));
					if (!isNaN(mainId))
					{
						cid = mainId + offsetM;
						result = true;
						if (!mainPos)
						{
							mainPos = 0;
						}
					}
					if (!isNaN(mainPos))
					{
						result = true;
						if (cid == pCondition)
						{
							mainPos = mainPos + offsetP;
						}
					}
					if (result)
					{
						xml['@m'] = cid;
						xml['@t'] = TimeFormat.ss2hhmmss((i == 0) ? 0 : mainPos, true);
					}
				}
			}
			
			file = new File(pathSting);
			fs.open(file, FileMode.WRITE);
			fs.writeMultiByte("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + data.toXMLString(), "utf-8");
			fs.close();
			//trace(pathSting.replace((/xml/,"$`"+new Date().time+)));
		}
	
	}

}